package com.handydata.antifraud.ws.filecheck;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import com.handydata.common.util.XMLUtil;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;



public class UserDomParser {

//    public static void main(String[] args) throws ParserConfigurationException,
//            SAXException, IOException {
//
//        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//        DocumentBuilder builder = factory.newDocumentBuilder();
//        String fileName = "D:/software/shbank20200511/shbank-risk/src/main/java/com/handydata/antifraud/ws/filecheck/xml/user.xml";
//
////        String fileName = "D:/software/shbank20200511/shbank-risk/src/main/java/com/handydata/antifraud/ws/filecheck/dtd/user.dtd";
//        File file = new File(fileName);
//        boolean validate = new XMLUtil().XMLValidateDTD(file);
//        if (validate) {
//            System.out.println("通过DTD校验！");
//            Document doc = builder.parse(file);
//            Element root = doc.getDocumentElement();
//            printElement(root);
//        } else {
//            System.out.println("该XML文件未通过DTD校验解析终止！！！");
//        }
//
//    }


    public static void printElement(Element e) {
        System.out.print("<" + e.getTagName());
        NamedNodeMap map = e.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            Attr attr = (Attr) map.item(i);
            System.out.print(" " + attr.getName() + "=\"" + attr.getValue()
                    + "\"");
        }
        System.out.print(">");
        NodeList list = e.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element en = (Element) n;
                printElement(en);
            } else {
                System.out.println(n.getTextContent());
            }
        }
        System.out.print("</" + e.getTagName() + ">");
    }

}
